package com.meepo.basic.schema.user

import com.meepo.basic.auth.Store
import java.io.Serializable

data class User(
    val id: String?,
    var nickname: String?,
    var avatar: String?,
    val phone: String?,
    var sign: String?,
    var gender: String?,
    var birthday: String?,
    var location: String?,
    val token: String?,
    private var cover: String?,
    private val tags: List<String>?,
    private var relationBetweenUs: List<String>?,
    val stat: Stat?
) : Serializable {

    companion object {
        const val SELF = "self"

        const val RELATION_TYPE_FOLLOW = "follow"
    }

//    fun getLocation(): String {
//        if (location == null) return ""
//
//        return location!!.province ?: "" + " " + location!!.city ?: ""
//    }

    fun isMe(): Boolean {
        Store.Func.getUserSummary()?.id?.let {
            return it == id
        }

        return false
    }

    fun getCover(): String {
        cover?.let {
            return it
        }

        avatar?.let {
            return it
        }

        return ""
    }

    fun getTag(): String? {

        tags?.let {
            if (it.isNotEmpty()) return it[0]
        }

        return null
    }

    val followed: Boolean
        get():Boolean = relationBetweenUs?.contains(RELATION_TYPE_FOLLOW) ?: false


    fun setFollowed(): User {

        if (followed) return this

        addRelation(RELATION_TYPE_FOLLOW)

        return this
    }

    fun setUnfollowed(): User {
        if (!followed) return this

        deleteRelation(RELATION_TYPE_FOLLOW)

        return this
    }


    private fun addRelation(relationType: String) {
        if (relationBetweenUs == null) {
            this.relationBetweenUs = listOf(relationType)
        } else {
            val old = HashSet(this.relationBetweenUs!!)
            old.add(relationType)

            this.relationBetweenUs = ArrayList(old)
        }
    }

    private fun deleteRelation(relationType: String) {
        if (relationBetweenUs != null) {
            val old = HashSet(this.relationBetweenUs!!)
            old.remove(relationType)
            this.relationBetweenUs = ArrayList(old)
        }
    }
}